home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 09 - 1993 / 09.04 Apr 93 / Creating EPSF Files / Main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-15  |  3.7 KB  |  180 lines  |  [TEXT/KAHL]

  1. /* Main.c */
  2. /* Convert PICT files to EPSF. */
  3. /* Copyright 1992, Gary D. McGath */
  4.  
  5. /* Mac Resource Defs */
  6.  
  7. #define APPLMENU 128
  8. #define FILEMENU 180
  9. #define WatchCursor 4
  10. #define NIL 0L
  11.  
  12. extern void DrawEPSF(PicHandle han, int theFile);
  13.  
  14. int main (void);
  15. void mainloop(void);
  16. void docommand(long mensel);
  17. void doconvert(void);
  18. PicHandle ReadPict(int theFile);
  19.  
  20.  
  21. extern int thePSResFile;
  22.  
  23. MenuHandle apmenu;
  24. MenuHandle filemenu;
  25. EventRecord myevent;
  26. Handle pictHan;
  27. WindowPtr whichwindow;
  28. int infile;
  29. int thePSFile;
  30. Point gfloc = {40,40};
  31. SFTypeList typelist = {'PICT'};    /* list of acceptable file types */
  32.  
  33. main()
  34. {
  35.  
  36.     InitGraf((Ptr)&thePort);
  37.     InitFonts();
  38.     InitWindows();
  39.     InitDialogs(0L);
  40.     InitMenus();
  41.     InitCursor();
  42.  
  43.     apmenu = GetMenu(APPLMENU);
  44.     AddResMenu(apmenu,'DRVR');
  45.     InsertMenu(apmenu,0);
  46.     filemenu = GetMenu(FILEMENU);
  47.     InsertMenu(filemenu,0);
  48.     DrawMenuBar();
  49.     mainloop();
  50. }
  51.  
  52.  
  53. void mainloop()
  54. {
  55.     int code;
  56.     int ch;
  57.  
  58.     for (;;) {
  59.         WaitNextEvent(everyEvent,&myevent,2L,NIL);
  60.         switch (myevent.what) {
  61.         case keyDown:
  62.             ch = myevent.message & charCodeMask;
  63.             if (myevent.modifiers & cmdKey)
  64.                 docommand(MenuKey(ch));
  65.             break;
  66.         case mouseDown:
  67.             code = FindWindow(myevent.where,&whichwindow);
  68.             switch (code) {
  69.             case inMenuBar:
  70.                 docommand(MenuSelect(myevent.where));
  71.                 break;
  72.             case inSysWindow:
  73.                 SystemClick(&myevent,whichwindow);
  74.                 break;
  75.             }
  76.         break;
  77.         }
  78.     }
  79. }
  80.  
  81.  
  82.  
  83. void docommand(long mensel)
  84. {
  85.     register short men, item;
  86.     char accname[64];        /* holder for desk accessory name */
  87.     GrafPtr saveport;
  88.  
  89.     men = mensel >> 16;        /* high order word is menu */
  90.     item = (short) mensel;    /* low order word is item */
  91.     if (men == 0)
  92.         return;                /* nothing to do */
  93.     if (men == APPLMENU) {
  94.         if (item <= 2)        /* "about" -- not implemented */
  95.             SysBeep(1);
  96.         else {
  97.                GetItem(apmenu,item,(StringPtr)accname);
  98.             GetPort(&saveport);    /* don't let desk acc bomb the port */
  99.             OpenDeskAcc((StringPtr)accname);
  100.             SetPort(saveport);
  101.         }
  102.     }
  103.     else switch(item) {        /* only other menu is File */
  104.     case 1:
  105.         doconvert();        /* to resource */
  106.         break;
  107.     case 3    :
  108.     default:
  109.         ExitToShell();
  110.     }
  111.     HiliteMenu(0);            /* clean up display */
  112. }
  113.  
  114.  
  115.  
  116. /* Command handler to convert PICT to EPSF. */
  117. void doconvert()
  118. {
  119.     SFReply myreply;    /* reply structure from open dialog */
  120.     unsigned char *pt;
  121.     OSErr errcod;
  122.     PicHandle picHan;
  123.  
  124.     /* specify and open input (PICT) file */
  125.     SFGetFile(gfloc,0L,0L,1,&typelist,0L,&myreply);
  126.     if (!myreply.good)
  127.         return;        /* cancelled out */
  128.     errcod = FSOpen(&myreply.fName[0],myreply.vRefNum,&infile);
  129.     if (errcod != noErr)
  130.         return;
  131.  
  132.     /* specify output file */
  133.     for (pt = &myreply.fName[1]; pt < &myreply.fName[32];)
  134.         *pt++ = 0;    /* clear out name buffer */
  135.     SFPutFile(gfloc,(StringPtr)"\pName of output file:",(StringPtr)"\pEPSF File",0L,&myreply);
  136.     if (!myreply.good) {
  137.         FSClose(infile);
  138.         return;        /* cancelled out */
  139.     }
  140.     SetCursor(*GetCursor(WatchCursor));    /* Go get lunch now */
  141.     errcod = CreateEPSFFile(&myreply, &thePSFile);
  142.     if (errcod == noErr) {
  143.         picHan = ReadPict(infile);
  144.         if (picHan == 0)
  145.             goto done;
  146.         AddResource(picHan,'PICT',256,"\p");
  147.         DrawEPSF(picHan, thePSFile);            /* generate the PostScript */
  148.     }
  149.  
  150. done:
  151.     if (infile)
  152.         FSClose(infile);
  153.     if (thePSResFile)
  154.         CloseResFile(thePSResFile);
  155.     if (thePSFile)
  156.         FSClose(thePSFile);
  157.     FlushVol(0,0);
  158.     InitCursor();
  159. }
  160.  
  161.  
  162.  
  163. /* Read the PICT into a PicHandle */
  164. PicHandle ReadPict(int theFile)
  165. {
  166.     long eofPos;
  167.     long count;
  168.     Handle theHan;
  169.     SetFPos(theFile, fsFromStart, 512L);        /* skip PICT header */
  170.     GetEOF(theFile, &eofPos);                    /* get file length */
  171.     count = eofPos - 512;
  172.     theHan = NewHandle(count);            /* try to allocate */
  173.     if (MemError())
  174.         return 0;
  175.     HLock(theHan);
  176.     FSRead(theFile, &count, *theHan);        /* read it in */
  177.     HUnlock(theHan);
  178.     return ((PicHandle) theHan);
  179. }
  180.